package com.topka.buynewcar.android.pages;

import io.appium.java_client.TouchAction;
import io.appium.java_client.android.AndroidDriver;

import java.util.List;

import org.openqa.selenium.WebElement;

import com.topka.buynewcar.base.BasePage;
import com.topka.buynewcar.utils.DeviceInfo;
import com.topka.buynewcar.utils.Utils;

public class NewBuyCarMainFragmentPage extends BasePage {

	private List<WebElement> hot_series;
	private WebElement bannerVP;
	private WebElement askPriceLayout;

	// launch_bargain_btn

	public NewBuyCarMainFragmentPage(AndroidDriver wd) {
		super(wd);
	}

	private void initHotSeries() {
		hot_series = findViewsById("content_layout");
	}

	public void clickHotSeries(int index) {
		if (index < 0 || index > hot_series.size())
			throw new IndexOutOfBoundsException();
		initHotSeries();
		hot_series.get(index).click();
	}

	public void swapBannerVp(int time) {
		bannerVP = findViewById("bannerVP");
		TouchAction ta = new TouchAction(wd);
		for (int i = 0; i < time; i++) {
			ta.press(bannerVP).moveTo(bannerVP, 0, -100).release();
			ta.perform();
		}
	}

	public void clickAskPriceLayout() throws Exception {
		// 向上滑动 适配小屏幕手机找不到比价按钮
		int sw = DeviceInfo.getWidth(wd);
		int sh = DeviceInfo.getHeight(wd);
		int height = findViewById("hot_series_sc").getSize().height;
		wd.swipe((sw / 2), (sh * 3 / 4), (sw / 2), (sh * 3 / 4 - height), 1500);
		Utils.sleep(3);
		int bannerH = findViewById("bannerVP").getSize().height;
		wd.swipe((sw / 2), (sh * 3 / 4), (sw / 2), (sh * 3 / 4 - bannerH), 1500);
		Utils.sleep(3);
		// 点击比价
		askPriceLayout = wd.findElementById("ask_price_layout");
		askPriceLayout.click();
	}

	public void swapBannerVpToBargain() {
		int height = findViewById("hot_series_sc").getSize().height;
		int sw = DeviceInfo.getWidth(wd);
		int sh = DeviceInfo.getHeight(wd);
		wd.swipe((sw / 2), (sh * 3 / 4), (sw / 2), (sh * 3 / 4 - height), 1000);
		bannerVP = findViewById("bannerVP");
		int bannerW = bannerVP.getSize().width;
		int bannerH = bannerVP.getSize().height;
		int x = bannerVP.getLocation().x;
		int y = bannerVP.getLocation().y;
		while (true) {
			try {
				findViewById("launch_bargain_btn").click();
				return;
			} catch (Exception e) {
				wd.swipe((x + bannerW - 10), (y + bannerH / 2), (x + 10),
						(y + bannerH / 2), 1000);
			}
		}
	}

}
